import api from '@/api/index.js'
import {
	changeKeyToValue
} from './tools'

const typeMapByInterface = {
	GetValueList: { //通用查询字典POST
		url: '/api/Dict/GetValueList',
		nameValue: [
			['key', 'text'],
			['value', 'value'],
		],
	},
	unitFrquSelector: { //零售价
		url: '/api/pharmacy/unitFrquSelector',
		nameValue: [
			['key', 'text'],
			['value', 'value'],
		],
	},
	dosformSelector: { //剂型选择器
		url: '/api/pharmacy/dosformSelector',
		nameValue: [
			['key', 'text'],
			['value', 'value'],
		],
	},
	supplierSelector: { //供应商选择器
		url: '/api/pharmacy/supplierSelector',
		nameValue: [
			['key', 'text'],
			['value', 'value'],
		],
	},

}



export default function getOptions(type, options, params, method = 'get') {
	const typeObj = typeMapByInterface[type]
	if (typeObj) {
		return api[method](typeObj.url, params).then((res) => {
			let {
				data
			} = res

			let result = changeKeyToValue(data, typeObj.nameValue)
			
			if (options) {
				options = result
			}

			return new Promise(function(resolve) {
				resolve(result)
			})
		})
	}
}